package com.log4jviewer;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * Model for keeping log4j-viewer's errors.
 * 
 * @author <a href="mailto:rd.ryly@gmail.com">Ruslan Diachenko</a>
 */
public final class ErrorsContainer {

    private Set<ErrorListener> listeners;

    private List<String> errors;

    public ErrorsContainer() {
        listeners = new HashSet<ErrorListener>();
        errors = new LinkedList<String>();
    }

    public void addError(final String error) {
        errors.add(error);
        notifyListeners(this);
    }

    public List<String> getErrors() {
        return errors;
    }

    public void removeAllErrors() {
        errors.clear();
    }

    public void notifyListeners(final ErrorsContainer event) {
        for (ErrorListener listener : listeners) {
            listener.handleErrorManagerEvent(event);
        }
    }

    public void addListener(final ErrorListener listener) {
        if (listener != null) {
            listeners.add(listener);
        }
    }

    public void removeListener(final ErrorListener listener) {
        listeners.remove(listener);
    }
}
